import Taro from '@tarojs/taro'

export const WXRequest = (url: string, method: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | undefined, data: object) => {
    return new Promise((resolve, reject) => {
      Taro.request({
        url: url,
        method: method,
        data: data,
        header: {
          AUTHORIZATION: 'Bearer ' + Taro.getStorageSync('token')
        }
      }).then(res => {
        if (res.data && res.data.code && res.data.code === 1) {
          // code: 0 = success, code: 1 = error
          Taro.atMessage({
            type: 'error',
            message: res.data.msg
          })
        }
        if (res.statusCode === 401) {
          Taro.redirectTo({url: '/pages/auth/MPLogin'})
          throw('没有权限')
        }
        if (res.statusCode >= 300) throw('请求出现异常')
        resolve(res)
      }).catch(e => {
        reject(e)
      })
    })
}